import { TweenFunc } from '../../Tween/TweenFunc';
import { handler } from '../../Global/Utils';
import { TweenUtil } from '../../Tween/TweenUtil';
//------------------------------------------------------------
// * Author         : Ekko
// * Description    : 弹窗管理类
//------------------------------------------------------------

type popUI = {
    node: cc.Node;
    is_show: boolean;
}

interface ui_transition{
    transType: ui_transition_type;
    tweenFunc?: Function;
    duration?: number;
}

export const enum ui_transition_type{
    NONE = 1, // 默认
    FADEIN, // 渐渐出现
    DROPDOWN, // 下拉
    POPUP, // 弹出
    LEFTIN, // 左滑入
    RIGHTIN, // 右滑入
}

export class PopUIMgr{
    private static inst: PopUIMgr;
    private ui_cache: any;
    private ui_stack: Array<string>;
    private ui_show_handler: handler;

    private constructor(){
        this.ui_cache = {};
        this.ui_stack = new Array<string>();
    }

    static getInst(): PopUIMgr{
        if(!this.inst){
            this.inst = new PopUIMgr();
        }
        
        return this.inst;
    }

    private getUI(path: string): popUI{
        let ui: popUI = this.ui_cache[path];
        if(!ui){
            this.ui_cache[path] = ui = {node: null, is_show: false};
        }
        return ui;
    }

    peek(){
        return this.ui_stack[this.ui_stack.length - 1];
    }

    clear(){
        for(let path in this.ui_cache){
            this.hide(path);
        }
        this.ui_cache = {};
        this.ui_stack.length = 0;
    }

    show(path: string, transition?: ui_transition, ...param: any[]): void{
        let ui: popUI = this.getUI(path);
        if(ui.is_show){
            return;
        }
        ui.is_show = true;

    }

    hide(path: string): void{
        let ui: popUI = this.ui_cache[path];
        if(!ui){
            return;
        }
        this.ui_cache[path] = null;
        ui.is_show = false;
        if(ui.node){
            
        }
    }

    applyTransitionEffect(node: cc.Node, transition: ui_transition){
        if(transition && transition.transType == ui_transition_type.NONE){
            return;
        }
        transition = transition || {
            transType: ui_transition_type.FADEIN,
            duration: 0.5,
            tweenFunc: TweenFunc.Linear
        };
        switch(transition.transType){
            case ui_transition_type.FADEIN:
                TweenUtil.from({node, duration: transition.duration || 1, opacity: 0, tweenFunc: transition.tweenFunc || TweenFunc.Linear});
        }
    }
}